// miniprogram/cardJudge/cardJudge.js
let app = getApp();
let cloudImage = require('../../utils/cloudImage');
var db = wx.cloud.database();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    visible: false,
    dialogImg: '',
    markText: '',
    markIndex: '',
    contentShow: '',
    comment: '',
    changeNum: 0,
    changeAct: 'star_choosed',
    starChoosed: '',
    star: '',
    items: [{}],
    hero: '',
    showTitle: '',
    scrollItems: [],
    cardifScroll: '0',
    selectEdition: '',
    title: ''
  },


  cardshortTap(e) {
    let { items } = this.data;
    var selectIndex = e.currentTarget.dataset.index;

    // console.log(items)
    wx.previewImage({
      urls: [items[selectIndex]['imageUrl']]
    });
  },

  childCardShortTap(e) {
    const {scrollItems}=this.data
    let { index} =e.currentTarget.dataset;
    wx.previewImage({
      urls: [scrollItems[index]]
    });
  },

  cardlongTap(e) {
    let that = this;
    let { items, scrollItems } = that.data;
    let selectIndex = e.currentTarget.dataset.index;
    const item = items[selectIndex]
    let comment = ''
    if (item.comment) {
      comment = item.comment
    }
    let arr = []
    if (item.childItem && item.childItem.length > 1) {
      arr = item.childItem.map(v => v.imageUrl)
    }
    that.setData({ visible: true, comment, scrollItems: arr, title: item.name, dialogImg: item.imageUrl,markIndex:selectIndex });
  },

  starChange(e) {
    const { items } = this.data
    const { index } = e.currentTarget.dataset;
    items[index].stars = e.detail
    this.setData({ items });
  },

  onCancel(e) {
    // console.log('取消评论')
    this.setData({ visible: false });
    //调用数据库评论
  },

  onConfirm(e) {
    // console.log('保存评论')
    let that = this;
    let { hero, markIndex, comment, items } = this.data;
    items[markIndex]['comment'] = comment;
    that.setData({ items });
    db.collection('userScore')
      .where({
        openId: app.globalData.openId
      })
      .update({
        data: {
          [hero[markIndex].comment]: comment
        },
        success: function (res) {
          // console.log(res)
          wx.showToast({
            title: '评论成功',
            icon: 'none',
            duration: 2000
          });
        },

        fail: function (res) {
          wx.showToast({
            title: '评论失败',
            icon: 'none',
            duration: 2000
          });
        }
      });
    that.setData({ visible: false });
  },

  bindTextAreaBlur(e) {
    this.setData({ comment: e.detail });
  },

  uploadData(selectEdition, data, hero) {
    db.collection('userScore')
      .where({
        openId: app.globalData.openId
      })
      .update({
        data: {
          [selectEdition]: data
          // [selectEdition[hero]]: items
        },
        success: function (res) {
          console.log(res1);
          wx.showToast({
            title: cloudImage.heroText(hero) + '提交成功',
            icon: 'none',
            duration: 2000
          });

          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            });
          }, 1500);
        },
        fail: console.error
      });
  },

  setCloudData(e) {
    let that = this;
    let { hero, items, selectEdition } = this.data;
    console.log(hero, items, selectEdition);
    let upDateData = { [hero]: items };
    console.log(app.globalData[selectEdition][hero]);
    // app.globalData['selectEdition'][hero]=items
    app.globalData[selectEdition][hero] = items;
    console.log(app.globalData['Barrens']);
    if (!app.globalData.openId) {
      wx.getUserProfile({
        desc: '正在获取', //不写不弹提示框
        success: function (res) {
          app.globalData.userInfo = res.userInfo;
          wx.cloud.callFunction({
            name: 'login',
            success: res2 => {
              var openid = res2.result.openid;
              app.globalData.openId = openid;
              db.collection('userScore')
                .where({
                  openId: app.globalData.openId
                })
                .get({
                  success: function (res2) {
                    // res.data 包含该记录的数据
                    console.log(res2.data[0]['openId']);
                    if (!res2.data[0]['openId']) {
                      db.collection('userScore').add({
                        data: {
                          openId: app.globalData.openId,
                          nickName: app.globalData.userInfo.nickName
                        },
                        success: function (res) {
                          db.collection('userScore')
                            .where({
                              openId: app.globalData.openId
                            })
                            .update({
                              data: {
                                [selectEdition]: upDateData
                                // [selectEdition[hero]]: items
                              },
                              success: function (res) {
                                console.log(res);
                                wx.showToast({
                                  title: cloudImage.heroText(hero) + '提交成功',
                                  icon: 'none',
                                  duration: 2000
                                });

                                setTimeout(function () {
                                  wx.navigateBack({
                                    delta: 1
                                  });
                                }, 1500);
                              },
                              fail: console.error
                            });
                        }
                      });
                    } else {
                      db.collection('userScore')
                        .where({
                          openId: app.globalData.openId
                        })
                        .update({
                          data: {
                            [selectEdition]: upDateData
                            // [selectEdition[hero]]: items
                          },
                          success: function (res) {
                            console.log(res);
                            wx.showToast({
                              title: cloudImage.heroText(hero) + '提交成功',
                              icon: 'none',
                              duration: 2000
                            });

                            setTimeout(function () {
                              wx.navigateBack({
                                delta: 1
                              });
                            }, 1500);
                          },
                          fail: console.error
                        });
                    }
                  },
                  fail: console.error
                });
            },
            fail: res => {
              console.log('登录失败', res);
            }
          });
        },
        fail: function (err) {
          wx.showToast({
            title: '你的评分将在你退出小程序时删除，感谢你对本程序的使用',
            icon: 'none',
            duration: 2000
          });
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            });
          }, 1500);
        }
      });
    } else {
      db.collection('userScore')
        .where({
          openId: app.globalData.openId
        })
        .update({
          data: {
            [selectEdition]: upDateData
            // [selectEdition[hero]]: items
          },
          success: function (res) {
            console.log(res);
            wx.showToast({
              title: cloudImage.heroText(hero) + '提交成功',
              icon: 'none',
              duration: 2000
            });

            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              });
            }, 1500);
          },
          fail: console.error
        });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log(options)
    let that = this;
    let cloudUrl = app.globalData.cloudUrl;
    let url = cloudUrl + 'stars/star_choosed.png';
    cloudImage.cardRequest(url, starChoosedFun);
    function starChoosedFun(res) {
      that.setData({ starChoosed: res.tempFilePath });
      // that.data.starChoosed=res.tempFilePath
    }
    let starurl = cloudUrl + 'stars/star.png';
    cloudImage.cardRequest(starurl, starFun);
    function starFun(res) {
      that.setData({ star: res.tempFilePath });
      // that.data.star=res.tempFilePath
    }
    // console.log(options.chooseEdition)
    let selectEdition = options.chooseEdition;
    that.setData({ selectEdition: options.chooseEdition });
    that.setData({ hero: options.hero });
    that.setData({ showTitle: cloudImage.heroText(options.hero) });
    // 要先增加个判断，先看score表里有没有该用户数据
    if (app.globalData.openId) {
      db.collection('userScore')
        .where({
          openId: app.globalData.openId
        })
        .get({
          success: res => {
            if (
              typeof res.data[0][selectEdition] !== 'undefined' &&
              typeof res.data[0][selectEdition][options.hero] !== 'undefined'
            ) {
              that.setData({ items: res.data[0][selectEdition][options.hero] });
            } else {
              db.collection('cardlist').get({
                success: res2 => {
                  let hero = options.hero;
                  let cardList = [];
                  let cardDir = res2.data[0][selectEdition][hero];
                  console.log(cardDir);
                  for (let i = 0; i < cardDir.length; i++) {
                    cardList.push({});
                    // console.log(cloudUrl + selectEdition + '/' + hero + '/' + cardDir[i]['name'] + '.png')
                    wx.cloud.downloadFile({
                      fileID:
                        cloudUrl +
                        selectEdition +
                        '/' +
                        hero +
                        '/' +
                        cardDir[i]['name'] +
                        '.png',
                      success: res3 => {
                        cardList[i].id = i;
                        cardList[i].name = cardDir[i]['name'];
                        cardList[i].imageUrl = res3.tempFilePath;
                        cardList[i].stars = '0';
                        cardList[i].comment = '';
                        cardList[i].childItem = [];
                        cardList[i].childItem.push({
                          imageUrl: res3.tempFilePath
                        });
                        if (typeof cardDir[i].childCard === 'object') {
                          for (
                            let j = 0;
                            j <= cardDir[i].childCard.length - 1;
                            j++
                          ) {
                            let img_url =
                              cloudUrl +
                              selectEdition +
                              '/' +
                              hero +
                              '/' +
                              cardDir[i].childCard[j] +
                              '.png';
                            cardList[i].childItem.push({ imageUrl: img_url });
                            wx.cloud.downloadFile({
                              fileID: img_url,
                              fail: res4 => {
                                cardList[i].childItem.remove({
                                  imageUrl: res4.tempFilePath
                                });
                                console.error;
                              }
                            });
                          }
                        }
                        // console.log(cardList)
                        that.setData({ items: cardList });
                      },
                      fail: console.error
                    });
                  }
                  // console.log(cardList)
                  // this.setData({items:res.data[index][hero]})
                },
                fail: console.error
              });
            }
            // this.setData({heroClass:res.data})
          },
          fail: console.error
        });
    } //if 判断
    else if (app.globalData[selectEdition][options.hero]) {
      // console.log('426')
      that.setData({ items: app.globalData[selectEdition][options.hero] });
    } else {
      // console.log('430')
      db.collection('cardlist').get({
        success: res2 => {
          let hero = options.hero;
          let cardList = [];
          let cardDir = res2.data[0][selectEdition][hero];
          // console.log(cardDir)
          for (let i = 0; i < cardDir.length; i++) {
            cardList.push({});

            wx.cloud.downloadFile({
              fileID:
                cloudUrl +
                selectEdition +
                '/' +
                hero +
                '/' +
                cardDir[i]['name'] +
                '.png',
              success: res3 => {
                cardList[i].id = i;
                cardList[i].name = cardDir[i]['name'];
                cardList[i].imageUrl = res3.tempFilePath;
                cardList[i].stars = '0';
                cardList[i].comment = '';
                cardList[i].childItem = [];
                cardList[i].childItem.push({ imageUrl: res3.tempFilePath });
                if (typeof cardDir[i].childCard === 'object') {
                  for (let j = 0; j <= cardDir[i].childCard.length - 1; j++) {
                    let img_url =
                      cloudUrl +
                      selectEdition +
                      '/' +
                      hero +
                      '/' +
                      cardDir[i].childCard[j] +
                      '.png';
                    cardList[i].childItem.push({ imageUrl: img_url });
                    wx.cloud.downloadFile({
                      fileID: img_url,
                      // success: res7=>{
                      //   cardList[i].childItem.push({ 'imageUrl':  res7.tempFilePath})
                      // },
                      fail: res4 => {
                        cardList[i].childItem.remove({
                          imageUrl: res4.tempFilePath
                        });
                        console.error;
                      }
                    });
                  }
                }
                // console.log(cardList)
                that.setData({ items: cardList });
                app.globalData[selectEdition][hero] = cardList;
              },
              fail: console.error
            });
          }
        },
        fail: console.error
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() { },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { }
});
